// 1.1 引入路由
import * as VueRouter from 'vue-router'

// 1.2 引入组件
import Home from '@/layout/Home.vue'
import Login from '@/views/Login/index.vue'
import Error from '@/views/Error/index.vue'


// 2.1 创建路由规则
const routes = [
    {
        path: '/',
        name: 'home',
        component: Home
    },
    {
        path: '/login',
        name: 'login',
        component: Login
    },
    {
        path: '/404',
        name: 'error',
        component: Error
    },
    {
        path: '/:pathMatch(.*)',
        redirect: '/404'
    }
]


// 3.1 创建路由实例
const router = VueRouter.createRouter({
    history: VueRouter.createWebHashHistory(),
    routes
})


// 不登陆就要拦截
router.beforeEach((to, form, next) => {
    const isLogin = localStorage.getItem('isLogin')
    if(to.path === '/' && !isLogin) {
        next('/login')
    } else {
        next()
    }
})

export default router